const {
  get
} = require("../../utils/http")

// pages/mine/minestar.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    thumbNums: 0,
    list: [],
    loading: false,
    loadMoreText: '点击加载更多',
    triggeredRefresh: false,
    pageOpt: {
      pageNum: 1,
      pageSize: 5,
      targetType: "industry"
    },
    pageTotal: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.submit = false
    if (options.type == 'industry') {
      wx.setNavigationBarTitle({
        title: '我的行业',
      })
    } else {
      wx.setNavigationBarTitle({
        title: '我的课程',
      })
    }
    this.setData({
      "pageOpt.targetType": options.type
    })
    this.loadList(true)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  onLike(e) {
    if (this.submit) {
      this.setData({
        thumbNums: 2,
      })
      return
    }
    this.submit = true
    const {
      id,
      index
    } = e.currentTarget.dataset
    const list = this.data.list
    const type = this.data.pageOpt.targetType
    const that = this
    get("/user/like", {
        targetType: type,
        id: id,
        likeStatus: 0
      })
      .then(res => {
        setTimeout(() => {
          list.splice(index, 1)
          that.setData({
            thumbNums: 2,
            list: list
          })
          that.submit = false
        }, 1000);
      }).catch(err => {
        wx.showToast({
          title: '网络异常,请稍后再试',
          icon: 'none'
        })
        that.submit = false
      })
  },
  onRefresh() {
    this.loadList()
  },

  onLoadMore(e) {
    this.loadList()
  },
  toDetail(e) {
    const {
      id
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/${this.data.type == 'industry'?'index':'course'}/detail?id=${id}`,
    })
  },
  loadList(init) {
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true,
      loadMoreText: '正在加载数据'
    })
    let pageNum = this.data.pageOpt.pageNum
    get('/user/list', this.data.pageOpt)
      .then(res => {
        const rows = res.rows || []
        let total = res.total || 0
        let text = '点击加载更多数据'

        if (total != 0) {
          total = Math.ceil(total / this.data.pageOpt.pageSize)
        }

        if (this.data.pageOpt.pageNum > total) {
          if (this.data.list.length == rows.length) {
            wx.showToast({
              title: '没有更多数据了',
              icon: 'none'
            })
            this.setData({
              triggeredRefresh: false,
              pageTotal: total,
              loading: false,
              loadMoreText: "没有更多数据了"
            })
            return
          } else {
            init = true
          }
        }

        if (rows.length > 0) {
          rows.forEach(row => {
            let imagestr = row.coverImage
            if (imagestr && imagestr.includes(",")) {
              row.coverImage = imagestr.split(",")
              return
            }
            if (imagestr) {
              row.coverImage = [imagestr]
              return
            }
            row.coverImage = []
          });
        }

        if (rows.length < this.data.pageOpt.pageSize) {
          text = "没有更多数据了"
        }

        let list = this.data.list
        if (init) {
          list = []
        }

        list.push(...rows)
        this.setData({
          list: list,
          loadMoreText: text,
          triggeredRefresh: false,
          "pageOpt.pageNum": pageNum + 1,
          pageTotal: total,
          loading: false
        })
      }).catch(err => {
        console.log(err)
        wx.showToast({
          title: '网络异常,请稍后再试',
          icon: 'none'
        })
        this.setData({
          loading: false,
          triggeredRefresh: false,
          loadMoreText: '网络异常,请稍后再试'
        })
      })
  }
})